package pfc.pfcjoc.core;

import static playn.core.PlayN.*;

import playn.core.Font;
import playn.core.GroupLayer;
import playn.core.PlayN;
import react.UnitSlot;
import tripleplay.ui.Background;
import tripleplay.ui.Button;
import tripleplay.ui.Group;
import tripleplay.ui.Interface;
import tripleplay.ui.Label;
import tripleplay.ui.Root;
import tripleplay.ui.Style;
import tripleplay.ui.Styles;
import tripleplay.ui.Stylesheet;
import tripleplay.ui.Style.TextEffect;
import tripleplay.ui.layout.AxisLayout;

public class MenuGameState extends GameState {
  private static final MenuGameState instance = new MenuGameState();
  private Interface iface;
  private GroupLayer layer;


  private MenuGameState() {
    layer = graphics().createGroupLayer();
    graphics().rootLayer().add(layer);

    // create our UI manager and configure it to process pointer events
    iface = new Interface();
    // pointer().setListener(iface.plistener);

    Styles buttonStyles = Styles.none().add(Style.BACKGROUND.is(Background.bordered(0xFFDDDDDD, 0xFF000000, 1).inset(5))).
                                addSelected(Style.BACKGROUND.is(Background.bordered(0xFFCCCCCC, 0xFF000000, 1).inset(5)));

    Stylesheet rootSheet = Stylesheet.builder().add(Button.class, buttonStyles).create();

    // create our demo interface
    Root root = iface.createRoot(AxisLayout.vertical().gap(15), rootSheet);
    root.setSize(graphics().width(), graphics().height());
    root.addStyles(Styles.make(Style.BACKGROUND.is(Background.solid(0xFFAAAAAA))));
    layer.add(root.layer);
    // layer.setTranslation(graphics().width()/4, graphics().height()/4);

    Group buttons;
    Label pfcLabel = new Label("PFC Joc").addStyles(Styles.none().add(
        Style.FONT.is(PlayN.graphics().createFont("Helvetica", Font.Style.BOLD, 48))).add(Style.COLOR.is(0xFF222222)).add(
        Style.TEXT_EFFECT.is(TextEffect.OUTLINE)));
    root.add(pfcLabel, buttons = new Group(AxisLayout.vertical().offEqualize()),
        new Label("Tecla ESC: tornar al menu inicial"),
        new Label("L'objectiu del joc és arribar a \"END\" per passar de nivell"),
        new Group(AxisLayout.horizontal().offStretch()).add(
            new Group(AxisLayout.vertical().offStretch()).add(
                new Label("JOC"),
                new Label("Fletxes: moviment jugador"),
                new Label("Barra d'espai: saltar")
              ).addStyles(Style.BACKGROUND.is(Background.bordered(0x22000000, 0xFF000000, 1).inset(10))),
            new Group(AxisLayout.vertical().offStretch()).add(
                new Label("EDITOR"),
                new Label("Mouse per seleccionar/moure objectes"),
                new Label("Mouse per triar opcions del panell")
              ).addStyles(Style.BACKGROUND.is(Background.bordered(0x22000000, 0xFF000000, 1).inset(10)))
          )
      );

    String path;
    // BOTO JUGAR
    Button button = new Button("JUGAR");
    path = new String("images/play.png");
    button.setIcon(assets().getImage(path));
    buttons.add(button);
    button.clicked().connect(new UnitSlot() {
      @Override
      public void onEmit() {
        log().info("click a jugar");
        enterPlayingMode();
      }

    });

    // BOTO EDITAR
    button = new Button("EDITAR");
    path = new String("images/edit.png");
    button.setIcon(assets().getImage(path));
    buttons.add(button);
    button.clicked().connect(new UnitSlot() {
      @Override
      public void onEmit() {
        log().info("click a editar");
        enterEditingMode();
      }
    });
  }


  public static MenuGameState getInstance() {
    return instance;
  }


  public void onEnteringMode() {
    graphics().rootLayer().clear();
    graphics().rootLayer().add(layer);
    // pointer().setListener(iface.plistener);
  }


  public void onLeavingMode() {
    graphics().rootLayer().remove(layer);
  }


  public void enterEditingMode() {
    EditGameState edit = EditGameState.getInstance();
    edit.setContext(context);
    context.setCurrentGameState(edit);
  }


  public void enterPlayingMode() {
    PlayGameState play = PlayGameState.getInstance();
    play.setContext(context);
    context.setCurrentGameState(play);
  }


  public void enterMenuMode() {
    log().info("Ja estem en MENU");
  }


  public void paint(float alpha) {
    if (iface != null) {
      iface.paint(alpha);
    }
  }


  public void update(float delta) {
    if (iface != null) {
      iface.update(delta);
    }
  }
}
